import  sqlite3

import fontTools.t1Lib
def printCookies(cookiesDB):
     try:
         conn = sqlite3.connect(cookiesDB)
         c = conn.cursor()
         c.execute('SELECT host, name, value FROM moz_cookies')
         print("\n[*] -- Found Cookies --")
         for row in c:
             host = str(row[0])
             name = str(row[1])
             value = str(row[2])
             print(f"[+] Host: {host}, Cookie Name: {name}, Value: {value}")
     except Exception as e:
         # 注意修正了'encyrpted'到'encrypted'的拼写错误
         # 并且提供了更通用的错误消息
         if 'encrypted' in str(e):
             print("\n[*] Error reading your cookies database")
             print("[*] This might be because the database is encrypted or damaged.")
         else:
             print(f"\n[*] An error occurred: {e}")


def printHistory(placesDB):
    try:
        conn = sqlite3.connect(placesDB)
        c = conn.cursor()
        # 修正SQL查询语句
        c.execute("""
            SELECT moz_places.url, datetime(moz_historyvisits.visit_date/1000000, 'unixepoch')
            FROM moz_places, moz_historyvisits
            WHERE moz_places.id = moz_historyvisits.place_id AND moz_historyvisits.visit_count > 0
        """)
        print("\n[*] -- Found History --")
        for row in c:
            url = row[0]
            date = row[1]
            # 修正打印语句中的分号
            print("[+] " + date + ' - Visited: ' + url)
    except sqlite3.Error as e:
        # 更通用的异常处理，针对SQLite错误
        print("\n[*] Error reading your places database:", e)
        # 这里通常不需要提示用户升级Python-sqlite3库，因为sqlite3是Python标准库的一部分
    except Exception as e:
        # 捕获其他非SQLite异常
        print("\n[*] An unexpected error occurred:", e)


def printGoogle(placesDB):
    try:
        conn = sqlite3.connect(placesDB)
        c = conn.cursor()
        # 修正SQL查询语句
        c.execute("""
            SELECT moz_places.url, datetime(moz_historyvisits.visit_date/1000000, 'unixepoch')
            FROM moz_places, moz_historyvisits
            WHERE moz_places.id = moz_historyvisits.place_id
        """)
        print("\n[*] -- Found History --")
        for row in c:
            url = row[0]
            date = row[1]
            # 修正打印语句中的分号
            if 'google' in url.lower():
                print(url)
    except sqlite3.Error as e:
        # 更通用的异常处理，针对SQLite错误
        print("\n[*] Error reading your places database:", e)
        # 这里通常不需要提示用户升级Python-sqlite3库，因为sqlite3是Python标准库的一部分
    except Exception as e:
        # 捕获其他非SQLite异常
        print("\n[*] An unexpected error occurred:", e)


def printCookies(cookiesDB):
    try:
        conn = sqlite3.connect(cookiesDB)
        c = conn.cursor()
        c.execute('SELECT host, name, value FROM moz_cookies')
        print("\n[*] -- Found Cookies --")
        for row in c:
            host = str(row[0])
            name = str(row[1])
            value = str(row[2])
            if "google" in host.lower():
                print(f"[+] Host: {host}, Cookie Name: {name}, Value: {value}")
    except Exception as e:
        # 注意修正了'encyrpted'到'encrypted'的拼写错误
        # 并且提供了更通用的错误消息
        if 'encrypted' in str(e):
            print("\n[*] Error reading your cookies database")
            print("[*] This might be because the database is encrypted or damaged.")
        else:
            print(f"\n[*] An error occurred: {e}")

def main():
    parser = optparse.OptionParser("usage%prog -p <E:\1\pythonProject1\python实验>")
    parser.add_option('-p',dest = 'pathName', type = 'string',
                      help = 'specify firefox profile path')
    (options ,args) = parser.parse_args()
    pathName = options.pathName
    if pathName == None:
        print(parser.usage)
        exit(0)
    if os.path.isdir(pathName) == False:
        print("[!] Path Does Not Exist:" + pathName)
        exit(0)
    cookiesDB = os.path.join(pathName,'cookies.sqlite')
    if os.path.isfile(cookiesDB):
        printCookies(cookiesDB)
    else:
        print('[!] Cookies Db does not exist:' + cookiesDB)
'''
    placesDB = so.path.join(pathName,'plcaes.sqlite)
    if os.path.isfile(plcaesDB):
        printHistory(placesDB)
        printGoogle(placesDB)
    else:
        print('[!] PlacesDb does not exist ' + placesDB)

'''
if __name__ =='__main__':
    main()

